package thread02;

/**
 * 定义生产者
 * @author if
 *
 */
public class Producer implements Runnable {

	private String name;
	private Storage storage = null;
	
	public Producer(String name, Storage storage) {
		this.name = name;
		this.storage = storage;
	}
	
	@Override
	public void run() {
		int i = 0;
		
		while (true) {
			System.out.println(name + "已经生产一个：id为" + i + "的商品");
			System.out.println("============================");
			
			Product product = new Product(i++);
			
			try {
				// 直接往BlockQueue中push就可以，如果满了自动阻塞。如果被消费了会自动调用notify方法通知等待线程继续生产
				storage.push(product);
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
